package com.huike.pmps.controller;

import com.huike.pmps.model.dto.common.PageInfo;
import com.huike.pmps.model.entity.Member;
import com.huike.pmps.model.entity.PO;
import com.huike.pmps.model.vo.Result;
import com.huike.pmps.service.POService;
import com.huike.pmps.utils.TokenUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * 作者：谌贵斌(黑桃K)
 * 日期: 2888-08-08
 * 老师QQ: 272488352
 * 官方QQ交流群: 377748272
 * 刀客程序员官网：http://www.daoke360.com
 * 刀客程序员淘宝旗舰店：https://daoke360.taobao.com/
 */
@RestController
@RequestMapping("/api/contracts")
@Api(tags = "新合同管理")
public class ContractController {

    @Autowired
    POService poService;


    @ApiOperation(value = "按搜索条件分页查询", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "customerName", value = "客户名称", paramType = "query", dataType = "String")
    })
    @GetMapping
    public Result selectByCondition(HttpServletRequest request, PageInfo pageInfo, String customerName) {
        //从请求头中获取token
        Member member = (Member) request.getSession().getAttribute(TokenUtil.getToken(request));
        return poService.selectByCondition(member, pageInfo, customerName);
    }


    @ApiOperation(value = "添加新合同", httpMethod = "POST")
    @PostMapping
    public Result insertOne(PO po) {
        return poService.insertOne(po);
    }

    @ApiOperation(value = "根据ID更新新合同数据", httpMethod = "PUT")
    @PutMapping
    public Result updateById(PO po) {
        return poService.updateById(po);
    }


    @ApiOperation(value = "导入PO管理列表，生成PO单号", httpMethod = "PATCH")
    @PatchMapping("/{id}")
    public Result generatorPO(@PathVariable("id") String ids) {
        return poService.updateAndGeneratorPO(ids);
    }

    @ApiOperation(value = "删除合同", httpMethod = "DELETE")
    @DeleteMapping("/{id}")
    public Result deleteById(@PathVariable("id") Integer id) {
        return poService.deleteById(id);
    }

}
